package com.lhh.company.website.config;

import com.lhh.company.website.entity.Res;
import com.lhh.company.website.entity.SysException;
import lombok.extern.log4j.Log4j2;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Log4j2
@RestControllerAdvice
public class GlobalExceptionHandler {
    // 处理自定义业务异常
    @ExceptionHandler(SysException.class)
    public Res<String> handleMyException(SysException ex) {
        return Res.fail(ex.getMessage());
    }

    // 处理自定义业务异常
    @ExceptionHandler(AuthenticationException.class)
    public Res<String> handleAuthenticationException(AuthenticationException ex) {
        return Res.fail(ex.getMessage());
    }

    // 兜底异常处理器
    @ExceptionHandler(Exception.class)
    public Res<String> handleException(Exception ex) {
        log.error("系统异常: ", ex);
        return Res.fail("服务内部错误");
    }
}
